#ifndef _RMS_TOML_ //- need for debug

#include "log.h"


string strtime()
{
  stringstream ss;
  ss << '[' << (SystemTime::get_cur_time()).get_string() << "] ";
  return ss.str();
};

CLog& operator<< (CLog& log, const string& arg)
{
  string res = arg;
  if(log.write0D)
  {
    int i; res = "";

    for(i=0; i<arg.length(); i++)
    if(arg[i]==0x0A)
      res = res+ "\r\n";
    else
      res = res + arg[i];
  }
  log.write(res);
  return log;
};

CLog& operator<< (CLog& log, char*& arg)
{
  string s(arg);
  return log << s;
};

// === misc functions ===

string MakeUpper(string s)
{
  int i;
  for(i=0; i<s.length(); i++)
    s[i] = toupper(s[i]);
  return s;
}

string inttostr(const int x)
{
  ostringstream os;
  os << x;
  return os.str();
}

int WritePascalString(BYTE* to, string s)
{
  int l = s.length();
  if(l>255) l=255;
  to[0] = l;
  memcpy(&to[1], s.c_str(), l);
  return l+1;
}

int ReadPascalString(BYTE* from, string& s)
{
 int l = from[0];
 s = string((char*)&from[1], l);
 return l;
}
// === time functions ===
#define FT2I64(ft) (((__int64)ft.dwLowDateTime) + (((__int64)ft.dwHighDateTime) << ((__int64)(32))))
double DiffTime(const FILETIME ft2, const FILETIME ft1)
{
  return (FT2I64(ft2) - FT2I64(ft1) ) / 1e7;
}
FILETIME AddTime(const FILETIME ft1, const double delta)
{
  __int64 t = FT2I64(ft1);
  t+= (__int64)(delta * 1e7);
  FILETIME res;
  res.dwHighDateTime = t >> ((__int64) 32);
  res.dwLowDateTime = t & 0xffffffff;
  return res;
}
FILETIME GetNowTime()
{
  FILETIME f;
  SYSTEMTIME s;
  GetLocalTime(&s);
  SystemTimeToFileTime(&s,&f);
  return f;
}
string GetTimeStr(const FILETIME ft)
{
  SYSTEMTIME st;
  FileTimeToSystemTime(&ft,&st);
  char buf[32]; string s;
  sprintf(buf,"%d",st.wYear); s = string(buf) + ".";
  sprintf(buf,"%02d",st.wMonth); s += string(buf) + ".";
  sprintf(buf,"%02d",st.wDay); s += string(buf) + " ";
  sprintf(buf,"%02d",st.wHour); s += string(buf) + ":";
  sprintf(buf,"%02d",st.wMinute); s += string(buf) + ":";
  sprintf(buf,"%02d",st.wSecond); s += string(buf);
  sprintf(buf,"%03d",st.wMilliseconds); s += "."+string(buf);
  return s;
}
#undef FT2I64


#endif